<template>
	<view>
		<view class="companyMessage">
			<view style="font-family: SimHei;">
				<h3>公司信息</h3>
			</view>
			<view class="companyName" @click="newCompanyone">
				<text>公司名称</text>
				<view class="">
					<input type="text" v-model="companyName" placeholder="请输入公司名称">
				</view>
			</view>
			<!-- 提示信息 -->
			<view class="companyName" v-if="fication==1">
				<text class="prompt">您选择加入的企业需要管理员认证，注册完成后请等待企业操作</text>
			</view>
			<view class="setCompany">
				<view>
					<checkbox-group @change="checkboxChange">
						<checkbox style="transform: scale(0.8)" class="round red" value="1"> <text
								style="font-size: 35rpx;">设为默认公司</text>
						</checkbox>
					</checkbox-group>
				</view>
				<text @click="emptyAll">清空当前信息</text>
			</view>

			<!-- 点击取消和确定 -->
			<view class="cancelDetermine">
				<view @click="cancelTwo"><text>取消</text></view>
				<view @click="sureBao" :style="{color:activeStyle.color,background:activeStyle.background}">
					<text>确定</text>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				fication: '',
				appId: '',
				androidIos: '',
				account: '',
				id: '',
				name: '',
				companyName: '', //注册公司名称
				activeStyle: {
					color: '#454545',
					background: '#F6F6F6'
				}
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		watch: {
			companyName(newCompany, oldCompany) {
				if (this.companyName != '') {
					this.activeStyle.color = '#fff';
					this.activeStyle.background = '#9e001a';
				} else {
					this.activeStyle.color = '#454545';
					this.activeStyle.background = '#F6F6F6';
				}

			}
		},
		onShow(e) {
			this.id = this.userInfo.id;
			this.name = this.userInfo.name;
			this.account = this.userInfo.account
		},
		onLoad() {
			this.androidIos = getApp().globalData.androidIos
		},
		methods: {
			...mapMutations(['setUserInfo']),
			// 点击你要选择的公司,渲染到input上面
			newCompanyone() {
				this.companyName = '';
				this.fication = 0
				uni.navigateTo({
					url: '../../company/company'
				});
			},
			checkboxChange(e) {
				this.flseTrue = e.detail.value.includes('1')
				console.log('========chen', this.flseTrue);
			},
			// 点击保存
			sureBao() {
				this.companyNull();
				if (this.flseTrue == true) {
					// false是没有设为默认公司
					let data = {
						userId: this.id,
						account: this.account,
						name: this.name,
						corpName: this.companyName,
						corpId: this.companyId
					};
					console.log(data, 'rskjhgdfuekwghli');
					this.$httpNew.post('authApi/CorpUser/JoinCorp', data).then(res => {
						if (res == null) {
							this.$httpNew.post('authApi/auth/GetUserInfo', {
								appId: this.androidIos
							}).then(res => {
								this.setUserInfo(res);
								this.$httpNew.post('authApi/user/ChangeCorp', {
										corpId: this.companyId.toString(),
										corpName: this.companyName.toString()
									})
									.then(res => {
										console.log(res, 'res子页面')
										let userInfo = this.userInfo;
										if (res == null) {
											userInfo.companyId = data.CompanyId;
											userInfo.companyName = data.CompanyName;
											this.setUserInfo(userInfo);
											uni.navigateBack({
												delta: 2
											});
										}
										uni.showToast({
											title: '绑定成功',
											icon: 'none'
										});
									});
							});
						}
					});

				} else {
					this.companyNull();
					let data = {
						userId: this.id,
						account: this.account,
						name: this.name,
						corpName: this.companyName,
						corpId: this.companyId
					};
					console.log(data, '134');
					this.$httpNew.post('authApi/CorpUser/JoinCorp', data).then(res => {
						console.log(res, '加入企业');
						if (res == null) {
							this.$httpNew.post('authApi/auth/GetUserInfo', {
								appId: '8DA58A92-B41F-46D6-9CD9-7983723BB5A5'
							}).then(res => {
								this.setUserInfo(res);
								uni.navigateBack({
									//返回
									delta: 1
								});
								uni.showToast({
									title: '加入成功',
									icon: 'none'
								});

							}).catch(err => {
								console.log(err, 'hkegfuiahs');
							});
						}
					});
				}
			},
			companyNull() {
				if (this.companyName == '') {
					uni.showToast({
						title: '选择的公司不能为空',
						icon: 'none'
					})
					return;
				}
			},
			// 点击清空输入的消息
			emptyAll() {
				this.fication = '';
				this.companyName = '';
			},
			// 点击取消返回上一级
			cancelTwo() {
				uni.navigateBack({
					url: './user_firmanage '
				})
			}
		}
	};
</script>
<style>
	page {
		height: 100%;
		background-color: #FFFFFF;
	}
</style>

<style lang="less" scoped>
	.companyMessage {
		width: 100%;
		border-radius: 10rpx;
		background-color: #fffFFF;
		display: flex;
		flex-direction: column;
		align-items: center;

		>view {
			width: 95%;
			line-height: 100rpx;
			// background-color: aquamarine;
		}

		>view:nth-child(1) {
			margin-top: 20rpx;
		}

		>view:nth-child(2) {
			border-bottom: 1px solid #F1F1F1;
		}

		>view:nth-child(4) {
			line-height: 80rpx;
			height: 80rpx;
		}
	}

	.companyName {
		display: flex;
		justify-content: space-between;

		.prompt {
			font-size: 24rpx;
			color: #b5001e;
		}

		>view {
			// background-color: aqua;
			width: 80%;

			>input {
				height: 100%;
				// background-color: red;
				text-align: left;
			}
		}
	}

	// 详细地址,设为默认公司页面布局
	.setCompany {
		// margin-top: 20rpx;
		text-align: right;
		display: flex;
		align-items: center;
		justify-content: space-between;


		>view {
			height: 100rpx;
			margin-left: -20rpx;
			// background-color: aqua;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
		}

		>text:hover {
			color: #b5001e;
		}
	}

	// 点击取消和确定
	.cancelDetermine {
		margin-top: 300rpx;
		display: flex;
		justify-content: space-between;
		// background-color: aqua;

		>view {
			text-align: center;
			width: 45%;
			border-radius: 50rpx;
		}

		>view:nth-child(1) {
			border: 1px solid #B0B0B0;
		}

		>view:nth-child(2) {
			color: #ffffff;
			background-color: #cccccc;
		}

		>view:nth-child(2):active {
			background-color: #9e001a;
		}
	}
</style>
